<?php
/**
* @file Rssm.php
* @synopsis  用户订阅
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-03-24 16:52:13
*/


class Invitem extends CI_Model
{
    function __construct()
    {
    }
    //将邀请写入数据库
    public function add_invite_db($user_id,$invite_ids,$post_id,$username,$type=0)
    {
        $query = array();
        $query['user_id'] = $user_id;
        $query['post_id'] = $post_id;
        $query['username'] = $username;
        $query['status'] = 0;
        $query['type'] = $type;

        foreach($invite_ids as $invite_id)
        {
            $query['invite_id'] = $invite_id;
            try
            {
                $result = $this->db->insert("ml_invite",$query);
            }
            catch(Exception $e)
            {
                echo $e->getMessage();
                //$result = false;
            }
        }
        return $result;
    }
	public function invite_list_db($post_id)
	{
		try
		{
			$result = array();
			$invites = array();
			
			$query = $this->db->select("invite_id,user_id")->where(array("post_id"=>$post_id))->get("ml_invite");
			$data = $query->result_array();
			$myInvites = array();
			foreach ($data as $oneInvite)
            {
				$invite_id = $oneInvite['invite_id'];	
				$user_id = $oneInvite['user_id'];
				if($this->uid == $user_id)
				{
					$myInvites[] = $invite_id;
				}
				if(array_key_exists($invite_id, $invites))
				{
					$invites[$invite_id] += 1;
				}
				else
				{
					$invites[$invite_id] = 1;
				}
			}
			$result['invites'] = $invites;
			$result['myInvites'] = $myInvites;
			
		}
		catch(Exception $e)
		{
			$result = array();	
		}
        return $result;
	}

    //谁发起了邀请
    public function invite_related_uids($post_id)
    {
        $query = $this->db->select("user_id")->where(array("post_id"=>$post_id))->get("ml_invite");
        $data = $query->result_array();
        $send_uids = array();

        foreach($data as $value)
        {
            $user_id = $value['user_id'];
            if(!in_array($user_id,$send_uids))
            {
                $send_uids[] = $user_id;
            }
        }

        return $send_uids;
    }

    //谁邀请了$receive_uid
    public function invite_send_uid($post_id, $receive_uid)
    {
        $query = $this->db->select("user_id")->where(array("post_id"=>$post_id, "invite_id"=>$receive_uid))->get("ml_invite");
        $data = $query->result_array();
        $uids= array();
        foreach($data as $value)
        {
            $user_id = $value['user_id'];
            $uids[] = $user_id;
        }
        return $uids;
    }


    //$user_id 邀请了谁
    public function invite_list_byuid($post_id, $user_id)
    {
        $query = $this->db->select("invite_id")->where(array("post_id"=>$post_id, "user_id"=>$user_id))->get("ml_invite");
        $data = $query->result_array();
        $uids= array();
        foreach($data as $value)
        {
            $user_id = $value['invite_id'];
            $uids[] = $user_id;
        }
        return $uids;
    }

    //谁邀请了$receive_uid 还有未回答的
    public function invite_send_uid_not_answer($post_id, $receive_uid)
    {
        $query = $this->db->select("user_id")->where(array("post_id"=>$post_id, "invite_id"=>$receive_uid,"is_comment"=>0))->get("ml_invite");
        $data = $query->result_array();
        $uids= array();
        foreach($data as $value)
        {
            $user_id = $value['user_id'];
            $uids[] = $user_id;
        }
        return $uids;
    }

    //更新邀请状态 
    public function update_invite_status($post_id, $receive_uid)
    {
        $set = array('is_comment'=>1);
        $where = array('post_id'=>$post_id,'invite_id'=>$receive_uid);
        return $return = $this->db->set($set)->where($where)->update('ml_invite');
    }

    /**
     * 获取推送列表
     * @return array
     */
    public function invite_list_for_push()
    {
            
        //版主 专家邀请专家可以发送 普通用户邀请专家不发送   每次处理100条
        $this->db->select("a.invite_id,a.user_id,a.post_id,a.username,b.admin_level as b_admin_level,b.user_level as b_user_level,c.user_level as c_user_level");
        $this->db->from("ml_invite as a");
        $this->db->join("ml_fellow as b","a.user_id=b.id","left");
        $this->db->join("ml_fellow as c","a.invite_id=c.id","left");
        $this->db->where(array("a.status"=>0,"a.type"=>0));
        $this->db->order_by("a.id","desc");
        $this->db->limit(100);

        $data = $this->db->get()->result_array();

        $return = [];
        foreach ($data as $item)
        {
            //不是专家 不是版主 不能邀请专家
            if($item['b_admin_level'] == 0 && $item['b_user_level'] == 0 && $item['c_user_level'] == 5)
            {
                continue;
            }
            else
            {
                $return[] = $item;
            }
        }

        return $return;
    }
    
    public function updatestatus($invite_id,$user_id,$post_id)
    {
        $set = array("status"=>1);
        $where = array("invite_id"=>$invite_id,"user_id"=>$user_id,"post_id"=>$post_id);
        return $this->db->update("ml_invite",$set,$where);
    }
}
